package com.automannn.cloud.practice.serviceConsumer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author automannn
 * @Date 2023/5/5
 */
@SpringBootApplication
@EnableFeignClients
public class ServiceConsumerApp {

    public static void main(String[] args) {
        ApplicationContext context= new SpringApplicationBuilder()
                .web(WebApplicationType.SERVLET)
                .properties("spring.config.name:consumer")
                .sources(ServiceConsumerApp.class)
                .build().run();

        HelloServiceClient helloService= context.getBean(HelloServiceClient.class);
        System.out.println(helloService.hello());
        System.out.println(helloService.hello());
    }

    @FeignClient(name = "automannn-provider",path = "/automannnProvider",fallback = HelloServiceCallback.class)
    public static interface HelloServiceClient {

        @RequestMapping("/hello")
        String hello();
    }

    @Component
    public static class HelloServiceCallback implements HelloServiceClient{

        @Override
        public String hello() {
            return "this is fallback";
        }
    }
}
